package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actsevenreward;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;

public class ActiveModule_七日登陆 extends PlayerModule {

    // bean ------------------------------------
    public static final int acttype = 4;

    private int logindays;        //登陆天数
    private ArrayList<Integer> getdays = new ArrayList<Integer>();    //领取天数
    private DateTime logintime;        //登陆时间  转天更新
    private int sid;    //

    public int getLogindays() {
        return logindays;
    }

    public void setLogindays(int logindays) {
        this.logindays = logindays;
    }

    public ArrayList<Integer> getGetdays() {
        return getdays;
    }

    public void setGetdays(ArrayList<Integer> getdays) {
        this.getdays = getdays;
    }

    public DateTime getLogintime() {
        return logintime;
    }

    public void setLogintime(DateTime logintime) {
        this.logintime = logintime;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    // fun ----------------------------------------------

    /**
     * @param pcode
     * @param serverid
     * @return
     */
    public static ActiveModule_七日登陆 create(String pcode, int serverid) {
        ActiveModule_七日登陆 am = new ActiveModule_七日登陆();
        am.setPlayerCode(pcode);
        am.setLogindays(0);
        am.setSid(serverid);
        am.set();
        return am;
    }

    /**
     * 刷新活动
     *
     * @param serverid
     */
    private void refresh(int serverid) {
        this.setLogindays(0);
        this.setGetdays(new ArrayList<Integer>());
        this.setLogintime(new DateTime(0));
        this.setSid(serverid);
        this.set();
    }

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

    /**
     * 能否领取某个奖励
     *
     * @param id
     * @return
     */
    public int cangainreward(int id) {
        int logindays = getLogindays();
        ArrayList<Integer> getdays = getGetdays();
        if (id > logindays) {
            return DrawStatus.cannotget.getStatus();
        }
        if (getdays.contains(id)) {
            return DrawStatus.hadget.getStatus();
        }
        return DrawStatus.canget.getStatus();
    }

    /**
     * 是否有可领取的奖励
     *
     * @return
     */
    public boolean cangainreward() {
        return getGetdays().size() < getLogindays() ? true : false;
    }


    /**
     * 获取某天的奖励信息
     *
     * @param id
     * @return
     */
    public ArrayList<Currency> gainreward(int id) {
        actsevenreward sevenreward = ActDataCenter.getData(id, actsevenreward.class, getPlayerCode());
        if (sevenreward == null) {
            return null;
        }
        return (ArrayList<Currency>) Currency.create(sevenreward.getRewardtype(), sevenreward.getRewardid(), sevenreward.getRewardnum());
    }

    /**
     * 领取奖励am处理
     *
     * @param id
     */
    public void dealgainreward(int id) {
        getGetdays().add(id);
        set();
    }

    // redis ---------------------------------------------------------

    static private String key(String playerCode) {
        return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
    }

    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            check();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取ActiveModule_七日登陆
     *
     * @param playerCode
     * @return
     */
    static public ActiveModule_七日登陆 get(String playerCode, Boolean isNewPlayer) {
        try {
            ActiveModule_七日登陆 module = getv(key(playerCode));
            PlayerBean pb = PlayerBean.get(playerCode);
            int serverid = pb.getServerId();
            if (module == null) {

                //新用户不需要去查数据库
                if (!isNewPlayer) {
                    module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_七日登陆.class);
                    if (module != null) {
                        module.set();
                    }//同步到redis
                }

                if (module == null) {
                    module = create(playerCode, serverid);
                }
            }

            if (module == null) {
                module = create(playerCode, serverid);
            }

            if (serverid != module.getSid()) {
                module.refresh(serverid);
            }

            module.update();
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private void update() {
        int logindays = getLogindays();
        if (logindays >= 7) {
            return;
        }

        if (getLogintime() == null) {
            logindays = 1;
            setLogindays(logindays);
            setLogintime(DateTime.now());
            set();
            return;
        }

        DateTime ltime = getLogintime();
        DateTime ntime = DateTime.now();
        int days = ActiveUtil.days(ltime, ntime);
        if (days >= 1) {
            logindays = logindays + 1;
            setLogindays(logindays);
            setLogintime(DateTime.now());
        }
        set();
    }

    /**
     * 查看是否有可领取奖励 控制红点
     */
    public void check() {
        if (getGetdays().size() == 7) {
            new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.领完了);
        } else {
            new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
        }

        if (cangainreward()) {
            new TipHandle().updateTip(getPlayerCode(), TipType.活动_七日登陆奖励, true);
        } else {
            new TipHandle().updateTip(getPlayerCode(), TipType.活动_七日登陆奖励, false);
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                ActiveModule_七日登陆 activeModule_七日登陆 = getv(key(playerCode));
                MySQLCacheDAO.setV(key, activeModule_七日登陆);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

}
